<template>
  <div class="app-container">
    <!-- 查询条件区域-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-="width:80px">
      <el-form-item label="机器人编号" prop="robot_num">
        <el-input v-model="queryParams.robot_num" placeholder="请输入机器人编号" clearable @keyup.enter.native="handleQuery"
          size="small" />
      </el-form-item>
      <el-form-item label="机器人名称" prop="robot_name">
        <el-input v-model="queryParams.robot_name" placeholder="请输入机器人名称" clearable @keyup.enter.native="handleQuery"
          size="small" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--增删改查按钮区域-->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"  id="addid"
          v-hasPermi="['nssc:robotconfig:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['nssc:robotconfig:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['nssc:robotconfig:remove']">删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">
        <el-button icon="el-icon-zoom-out" size="mini"  @click="handleclosedall">断开所有</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--  数据列表区域-->
    <el-table v-loading="loading" :data="robotconfigList" @selection-change="handleSelectionChange"
      :cell-style="{ borderColor: 'rgb(1 5 43)' }" :header-cell-style="{ borderColor: 'rgb(1 5 43)' }">
      <el-table-column type="selection" align="center" />
      <el-table-column label="状态" align="center" prop="robot_state" width="70px">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.robot_state == 1"
            style="font-size: 12px!important;height: 26px!important;line-height: 26px!important;display:inline-block!important;border-radius:4px!important;background: #20c013!important;border: 1px solid #024697!important;color: #fff!important">在线</el-tag>
          <el-tag v-else
            style="font-size: 12px!important;height: 26px!important;line-height: 26px!important;display:inline-block!important;border-radius:4px!important;background:#6c6b6b!important;border: 1px solid #024697!important;color: #fff!important">离线</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="机器人编号" align="center" prop="robot_num" width="155px"/>
      <el-table-column label="机器人名称" align="center" prop="robot_name" width="155px" />
      <el-table-column label="机器人ip" align="center" prop="robot_ip" width="155px" />
      <el-table-column label="摄像头IP" align="center" prop="video_ip1" width="155px" />
      <el-table-column label="MAC地址" align="center" prop="mac" />
      <el-table-column label="创建时间" align="center" prop="creater_time" width="170px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.creater_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-zoom-out" @click="handleclosed(scope.row)"
            v-hasPermi="['nssc:colsejqrconn:edit']">断开</el-button>
          <el-button size="mini" type="text" icon="el-icon-zoom-in" @click="handleopen(scope.row)"
            v-hasPermi="['nssc:connjqr:edit']">连接</el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['nssc:robotconfig:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['nssc:robotconfig:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--列表分页区域-->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" style="background:rgb(1 5 43);" />
    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" @close="handleCancle" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="85px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="机器人名" prop="robot_name">
              <el-input v-model="form.robot_name" placeholder="请输入机器人名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机器人IP" prop="robot_ip">
              <el-input v-model="form.robot_ip" placeholder="请输入机器人ip" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="机器人端口号" prop="robot_port">
              <el-input v-model="form.robot_port" placeholder="请输入机器人port" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头IP" prop="video_ip1">
              <el-input v-model="form.video_ip1" placeholder="请输入摄像头IP" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头端口号"  prop="video_port1">
              <el-input v-model="form.video_port1" @input="limitInput" placeholder="请输入摄像头端口号(默认8000)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头用户名" prop="video_name1">
              <el-input v-model="form.video_name1" placeholder="请输入摄像头用户名" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="摄像头密码" prop="video_pwd1">
              <el-input v-model="form.video_pwd1" placeholder="请输入摄像头密码" />
            </el-form-item>
          </el-col>


          <el-col :span="12">
            <el-form-item label="MAC" prop="mac">
              <el-input v-model="form.mac" placeholder="请输入MAC地址" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容(0-255)"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  addRobotConfig,
  delRobotConfig,
  getRobotConfig,
  listRobotConfig,
  updateRobotConfig
} from "@/api/nssc/robotconfig/robotconfig";
import {jqrclosedCmd, jqrclosedCmdALL, jqropenCmd} from "@/api/nssc/modbus/modbus";
import online from '@/assets/imgs/p2.png';
import offline from '@/assets/imgs/p22.png';
import "@/assets/styles/robt-bg.css";
import $ from "jquery";
export default {
  data() {
    return {
      onpc: online,
      offpc: offline,
      fileName: '',
      localFilePath: '',
      title1: '',
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      robot_names: [],
      timer: null,//定时器
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      mac: "",//mac地址
      //机器人类型
      robotype: [
        { robot_type: 0, robot_type_name: "普通" },
        { robot_type: 1, robot_type_name: "救援" },
        { robot_type: 2, robot_type_name: "防爆" }
      ],
      //摄像头类型
      cameratype: [
        { camera_type: 0, camera_type_name: "大华" },
        { camera_type: 1, camera_type_name: "海康" },
        { camera_type: 2, camera_type_name: "吊舱" }
      ],
      //机器人配置表格数据
      robotconfigList: [],
      // 弹出层标题
      title: "",
      //是否显示弹出层
      open: false,
      // 是否显示弹出层
      open1: false,
      queryparm: {
        plcid: undefined,
        robotid: undefined
      },
      //查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        robot_num: undefined,
        robot_name: undefined,
        id: undefined
      },
      // 表单参数
      form: {
        video_port1: '' // 输入框的值
      },
      // 表单校验
      rules: {
        robot_name: [
          { required: true, message: "机器人名称不能为空", trigger: "blur" }
        ],
        robot_ip: [
          { required: true, message: "机器人ip不能为空", trigger: "blur" }
        ],
        robot_port: [
          { required: true, message: "机器人port不能为空", trigger: "blur" }
        ],
        video_port1: [
          {
            required: true, message: "摄像头端口号只能有四位", trigger: "blur"
          }
        ],
        video_pwd1: [
          { required: true, message: "摄像头密码不能为空", trigger: "blur" }
        ],
        video_name1: [
          { required: true, message: "摄像头用户名不能为空", trigger: "blur" }
        ],
        video_ip1: [
          { required: true, message: "摄像头IP不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();    /** 初始化机器人列表 */
  },
  mounted() {
    this.timer = setInterval(() => {
      this.getList(); //刷新站点列表
      console.log("机器人在线离线定时器")
    }, 6000);//6秒
  },
  beforeDestroy() {
    //清除定时器
    if (this.timer) {
      clearInterval(this.timer); //关闭
    }
  },
  methods: {
    limitInput() {
      // 使用正则表达式限制输入为四位数
      this.form.video_port1 = this.form.video_port1.replace(/\D/g, '').slice(0, 4);
    },
    //自定义对话框关闭方法
    handleCancle() {
      this.$refs.form.resetFields()
      this.dialogVisible = false
    },
    /** 查询机器人配置列表 */
    getListsc() {
      this.loading = true;
      listRobotConfig(this.queryParams).then(response => {
        this.robotconfigList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询机器人配置列表 */
    getList() {
      this.loading = true;
      listRobotConfig(this.queryParams).then(response => {
        this.robotconfigList = response.rows;
        this.total = response.total;
        this.loading = false
        if (response.rows.length > 0) {
          $("#addid").css("display", "none");
        } else {
          $("#addid").css("display", "");
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancel1() {
      this.queryparm.robot_ip = undefined;
      this.queryparm.robot_port = undefined;
      this.fileName = undefined;
      this.open1 = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        creater_id: undefined,
        creater_time: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getListsc();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.robot_name = null;
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.robot_names = selection.map(item => item.robot_name)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {

      this.reset();
      this.open = true;
      this.title = "新增机器人";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRobotConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改机器人";
      });
    },
    //关闭jqr
    handleclosed(row) {
      this.queryparm.robotid = row.id;
      this.queryparm.robot_port = row.robot_port;
      jqrclosedCmd(this.queryparm).then(response => {
        if (typeof (response) != null && response != "" && response.msg != "") this.$modal.msg(response.msg);
      });
    },
    //关闭jqr所有
    handleclosedall() {
      jqrclosedCmdALL(this.queryparm).then(response => {
        if (typeof (response) != null && response != "" && response.msg != "") this.$modal.msg(response.msg);
      });
    },
    //打开jqr连接
    handleopen(row) {
      this.queryparm.robotid = row.id;
      this.queryparm.robot_ip = row.robot_ip;
      this.queryparm.robot_port = row.robot_port;
      jqropenCmd(this.queryparm).then(response => {
        if (typeof (response) != null && response != "" && response.msg != "") this.$modal.msg(response.msg);
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          //验证mac地址
          if (this.form.mac != null && this.form.mac != "") {
            let temp = /^[1-9]{1,2}:[1-9]{1,2}:[1-9]{1,2}:[1-9]{1,2}:[1-9]{1,2}:[1-9]{1,2}$/;
            if (!temp.test(this.form.mac)) {
              this.$modal.msgError("MAC地址冒号分割的6组数字组成。例：11:22:33:44:55:66");
              return false;
            }
          }

          if (this.form.id != undefined) {
            updateRobotConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRobotConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const robot_names = row.robot_name || this.robot_names
      this.$modal.confirm('是否确认删除机器人名称为"' + robot_names + '"的数据项？').then(function () {
        // this.$modal.confirm('是否确认删除选中的数据项？').then(function() {
        return delRobotConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep .el-col-12{
  height: 95px;
}

</style>
